package com.thread.demo.synchronize;

public class SynchronizedTest {

    public static void main(String[] args) {

        class Demo {

            public synchronized void synMethod() {
                for(int i=0; i<1000000; i++)
                    ;
            }

            public void synBlock() {
                synchronized( this ) {
                    for(int i=0; i<1000000; i++)
                        ;
                }
            }
        }

        Demo demo = new Demo();
        long start,diff;
        start = System.currentTimeMillis();
        demo.synMethod();                                 // 调用“synchronized方法块”
        diff = System.currentTimeMillis() - start;        // 获取“时间差值”
        System.out.println("synMethod() : "+ diff);


        start = System.currentTimeMillis();
        demo.synBlock();                                // 调用“synchronized方法块”
        diff = System.currentTimeMillis() - start;      // 获取“时间差值”
        System.out.println("synBlock()  : "+ diff);
    }
}
